<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class ToolClassController extends Controller
{
    //样品类型展示
    public function index(Request $request)
    {
        if ($request->isMethod('POST')) {
            $exist = DB::table('a_tool_class')->where('name', $request->name)->exists();
            if ($exist) {
                return out(0, '分类已存在');
            }
            $res = DB::table('a_tool_class')->insertGetId(['name' => $request->name]);
            $msg = $res ? '添加成功' : '添加失败';
            return out($res, $msg);
        }

        $data = DB::table('a_tool_class')->get();
        return view('admin.tool.class.index', compact('data'));
    }

    //样品类型 删除
    public function del(Request $request)
    {

        $id = $request->id;
        if ($id) {
            $exist = DB::table('a_tool')->where('tool_class_id', $id)->exists();
            if ($exist) {
                return out(0, '不能删除，当前分类下有工器具类型', ['time' => 2000]);
            }
            $res = DB::table('a_tool_class')->where('id', $id)->delete();
            return out($res, $res ? '删除成功' : '删除失败', ['time' => $res ? 1000 : 2000]);
        }
        return out(0, '请求错误', ['time' => 2000]);
    }

    //关联标准
    public function relStd(Request $request)
    {
        $id = $request->id; //工器具ID

        if ($request->isMethod('POST')) {
            $data = $request->only(['std_id', 'remark', 'cycle']);
            $data['tool_id'] = $id;
            // dd($data);
            try {
                //插入数据    insert 返回true 或 false
                $result = DB::table('a_std_tool_rel')->insert($data);
            } catch (\Throwable $e) {
                return '2';
            }
            //由于框架自身不支持响应bool值，所以转化一种形式 Exception
            return $result ? '1' : '0';
        }
        else {

            $data_std = DB::table('a_standard')->get();
            $data_tool = DB::table('a_tool')->find($id);

            $sql = "SELECT
            a_std_tool_rel.id,
            a_standard.num,
            a_std_tool_rel.cycle,
            a_std_tool_rel.remark,
            a_std_tool_rel.ctime
        FROM
            a_std_tool_rel
        LEFT JOIN a_standard ON a_std_tool_rel.std_id = a_standard.id
        WHERE
            a_std_tool_rel.tool_id = $id";

            $data_rel = DB::select($sql);
            return view('admin.tool.relStd', compact('data_std', 'data_tool', 'data_rel'));
        }
    }

    //关联参数（额定电压、分类）
    public function relParam(Request $request)
    {
        $std_tool_rel_id = $request->id; //标准与工器具关联 ID

        if ($request->isMethod('POST')) {
            $data = $request->only(['std_id', 'tool_id', 'type', 'val']);
            $data['std_tool_rel_id'] = $std_tool_rel_id;
            // dd($data);
            try {
                //插入数据    insert 返回true 或 false
                $result = DB::table('a_voltage_class')->insertGetId($data);
            } catch (\Throwable $e) {
                return '2';
            }
            //将排序字段更新为id
            DB::table('a_voltage_class')->where('id', $result)->update(['sort' => $result]);
            //由于框架自身不支持响应bool值，所以转化一种形式 Exception
            return $result ? '1' : '0';
        }
        else {

            $sql = "SELECT
                        A.id,
                        A.std_id,
                        A.tool_id,
                        B.num,
                        C.`name`
                    FROM
                        a_std_tool_rel A
                    LEFT JOIN a_standard B ON A.std_id = B.id
                    LEFT JOIN a_tool C ON A.tool_id = C.id
                    WHERE
                        A.id = :id";
            $info_data = DB::select($sql, ['id' => $std_tool_rel_id]);
            $info_data = $info_data[0];
            $data_rel = DB::table('a_voltage_class')->where('std_tool_rel_id', $std_tool_rel_id)->orderBy('type')->orderBy('sort')->get();
            return view('admin.tool.relParam', compact('info_data', 'data_rel'));
        }
    }

    //修改 关联参数（额定电压、分类）
    public function editParam(Request $request)
    {
        $vol_class_id = $request->input('id');

        if ($request->isMethod('POST')) {
            $data = $request->only(['ishide', 'sort']);

            try {
                //update 返回受影响的行数
                $result = DB::table('a_voltage_class')->where('id', $vol_class_id)->update($data);
            } catch (\Throwable $e) {
                return '2';
            }

            //由于框架自身不支持响应bool值，所以转化一种形式
            return $result >= 0 ? '1' : '0';
        }
        else {
            //查询信息
            $vol_class_info = DB::table('a_voltage_class')->find($vol_class_id);
            $data_tool = DB::table('a_tool')->find($vol_class_info->tool_id);

            return view('admin.tool.editParam', compact('vol_class_info', 'data_tool'));
        }
    }

    //关联标准 删除
    public function delRelStd(Request $request)
    {

        $id = $request->id;

        //  delete 返回受影响的行数
        $result = DB::table('a_std_tool_rel')->where('id', $id)->delete();
        //由于框架自身不支持响应bool值，所以转化一种形式
        return $result >= 0 ? '1' : '0';
    }

    //关联参数（额定电压、分类） 删除
    public function delParam(Request $request)
    {
        $id = $request->id;

        try {
            //  delete 返回受影响的行数
            $result = DB::table('a_voltage_class')->where('id', $id)->delete();
        } catch (\Throwable $e) {
            return '2';
        }
        //由于框架自身不支持响应bool值，所以转化一种形式
        return $result >= 0 ? '1' : '0';
    }

    /**
     * 排序 参数（额定电压、分类）
     */
    public function saveSortRelParam(Request $request)
    {
        $arr = $request->sort;
        if ($arr) {
            # 如果$arr 不为null 就遍历，遍历null会报错
            $resnum = 0;
            foreach ($arr as $key => $val) {
                $val = intval($val);
                $result = DB::table('a_voltage_class')->where('id', $key)->update(['sort' => $val]);
                $result ? $resnum += 1 : $resnum;
            }
            return response('您修改了' . $resnum . '条数据', 200);
        }
    }
}
